/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.action;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import vical.controller.EntryController;
import vical.model.Entry;
import vical.view.EntryDialog;
import vical.model.Internationalization;

/**
 * Action to add a new Entry
 * 
 * @author Can
 */
public class AddEntryAction extends AbstractAction {

    private EntryDialog entryDialog;

    public AddEntryAction(EntryDialog entryDialog) {
        super(Internationalization.getString("calendar_save_button"));
        putValue(MNEMONIC_KEY, Internationalization.getMnemonic("mnemonics_entry_dialog_done"));

        this.entryDialog = entryDialog;
    }

    public void actionPerformed(ActionEvent e) {
        Entry entry = entryDialog.getEntryFromInterface();
        String validateMessage = Validate(entry);
        if (validateMessage == "") {
            EntryController.addEntry(entry);
            entryDialog.dispose();
        } else {
            validateMessage = "Following errors have been occurred:\n"
                    + validateMessage
                    + "Please check them to continue";
            JOptionPane.showMessageDialog(entryDialog, validateMessage, "Insufficient information", JOptionPane.ERROR_MESSAGE);
        }
    }

    private String Validate(Entry entry) {
        String result = "";
        if (entry.getName().equals("")) {
            result += "• Entry should has a Name" + "\n";
        }
        if (entry.getCategory().equals("")) {
            result += "• Entry should has a Category" + "\n";
        }
        if ((entry.isAllday() && entry.getEndTime().before(entry.getStartTime()))
                || (!entry.isAllday() && !entry.getEndTime().after(entry.getStartTime()))) {
            result += "• End date should be later than Start date" + "\n";
        }

        return result;
    }
}
